package com.example.wszb.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


import com.example.wszb.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CameraActivity extends AppCompatActivity {
    ImageView imageView;
    Button takePic;
    public static final int TAKE_PHOTO=1;
    static final int REQUEST_IMAGE_CAPTURE=1;
    static final int RESULT_OK=-1;
    String path="";
    Uri contentUri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        imageView=findViewById(R.id.iv);
        takePic=findViewById(R.id.take_pic);
        takePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//开启系统相机的意图对象
                String imagePath= Environment.getExternalStorageDirectory()+"/DCIM";//设置照片保存路径
                File file=new File(imagePath, "default_image.jpg");
                //File file=createImageFile();
                path=file.getAbsolutePath();
                //使用文件提供者获取路径URL
                contentUri= FileProvider.getUriForFile(CameraActivity.this,"com.example.android.fileprovider",file);
                //设置传递参数 让相机拍照后保存照片
                intent.putExtra(MediaStore.EXTRA_OUTPUT,contentUri);
                startActivityForResult(intent,TAKE_PHOTO);
            }
        });
    }






    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case TAKE_PHOTO:
                if(resultCode == Activity.RESULT_OK) {
                    try{
                        Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(contentUri));
                        imageView.setImageBitmap(bitmap);

                    }
                    catch (FileNotFoundException e){
                        e.printStackTrace();
                    }
                }
                break;
            default:
                break;
        }

            //1.设置扫描意图
            Intent mediaScanIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            //2.设置路径

            File f=new File(path);
            //3.获取路径
            Uri contenturi=Uri.fromFile(f);//file:///storage/emulated/0/DCIM/default_image.jpg
            mediaScanIntent.setData(contenturi);
            System.out.println(contenturi.toString());
            //4.发送广播
            this.sendBroadcast(mediaScanIntent);
//            Bundle extras = data.getExtras();
//            Bitmap imageBitmap = (Bitmap) extras.get("data");
//            imageView.setImageBitmap(imageBitmap);
        }

    private File createImageFile() throws IOException {
        // 创建独一无二的文件名
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        path = image.getAbsolutePath();
        return image;
    }

}
